package  com.syqc.sensor.water

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.syqc.comlib.base.LazyLoadSirFragment
import com.syqc.comlib.callback.EmptyCallback
import com.syqc.comlib.databinding.IncludeRecyclerMathMBinding
import com.syqc.comlib.utils.GsonUtil
import com.syqc.comlib.utils.startActivityTo
import com.syqc.utils.Keys
import com.syqc.entity.ListInfo
import com.syqc.entity.OilDay
import com.syqc.entity.StatsType
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import  com.syqc.sensor.AddLeakActivity
import com.syqc.utils.DataUtil
import com.syqc.utils.ToolsUtil

class WaterInfoFragment :
    LazyLoadSirFragment<IncludeRecyclerMathMBinding>(IncludeRecyclerMathMBinding::inflate) {
    private lateinit var plate: String
    private lateinit var catId: String
    private var stats: StatsType? = null
    private val mAdapter by lazy { WaterInfoAdapter() }

    companion object {
        fun getInstance(plate: String, catId: String, index: Int): WaterInfoFragment {
            val fragment = WaterInfoFragment()
            val args = Bundle()
            args.putString(Keys.INTENT_PLATE, plate)
            args.putString(Keys.INTENT_CAR_ID, catId)
            args.putInt(Keys.INTENT_OTHER, index)
            fragment.arguments = args
            return fragment
        }
    }


    override fun initView(view: View) {
        plate = arguments?.getString(Keys.INTENT_PLATE) ?: ""
        catId = arguments?.getString(Keys.INTENT_CAR_ID) ?: ""
        val index = arguments?.getInt(Keys.INTENT_OTHER) ?: 0
        stats = DataUtil.dayStats?.get(index)
        initRecyclerView()
    }

    fun initRecyclerView() {
        binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
        binding.recyclerView.adapter = mAdapter
    }

    override fun initData() {
        stats?.let {
            if (it.info.size > 0) {
                loadService.showSuccess()
            } else {
                loadService.showCallback(EmptyCallback::class.java)
            }
            mAdapter.unit = it.unit
            mAdapter.precision = it.precision.toString()
            mAdapter.setList(it.info)
        }

    }

    override fun initEvent() {
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (ToolsUtil.isFastClick()) return@setOnItemChildClickListener
            val data = mAdapter.data[position]
            DataUtil.stats = data
            when (view.id) {
                R.id.lr_tv_water_add_times -> {
                    data.let {
                        if (it.upTimes > 0) {
                            if (data.list.isNullOrEmpty()) {
                                showToast(R.string.desc_toast_no_data)
                                return@setOnItemChildClickListener
                            }
                            handleAddLeak(data.list)
                            DataUtil.addLeak = addDetail
                            startActivityTo<AddLeakActivity>(requireActivity()) {
                                putExtra(Keys.INTENT_TITLE, plate)
                                putExtra(Keys.INTENT_OTHER, data.unit)
                                putExtra(Keys.INTENT_TYPE, 2)
                            }
                        }
                    }
                }

                R.id.iv_water_info_pop -> {
                    DataUtil.stats = data
                    getDayData(data)
                }
            }

        }
    }


    private fun getDayData(stats: OilDay) {
        isShowLoading(true)
        ServiceHelper.getRepository().getNodes(stats.carId, stats.day ?: "", stats.cfg ?: "")
            .autoDispose(scopeProvider).subscribe({
                if (it.size > 0) {
                    DataUtil.oilPints = it
                    startActivityTo<WaterChartActivity>(requireContext()) {
                        putExtra(Keys.INTENT_PLATE, plate)
                    }
                } else {
                    showToast(getString(R.string.toast_no_detail_data))
                }
                isShowLoading(false)

            }, {
                showToast(it.message ?: "")
                isShowLoading(false)
            })
    }

    override fun onDestroy() {
        super.onDestroy()
        DataUtil.oilPints = null
    }

    private val addDetail = ArrayList<ListInfo>()

    /**
     * 区分添加和漏油
     *
     */
    private fun handleAddLeak(list: String) {
        val infos = GsonUtil.formArrayList(list, ListInfo::class.java)
        addDetail.clear()
        infos.forEach {
            when (it.t) {
                "U" -> {
                    addDetail.add(it)
                }
            }
        }
    }
}